package com.hongyi.common.mongoDB.utils;

import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * MongoDB 分页工具类
 */
@Component
public class MongoPage<T> {

    private Integer pageSize;
    private Integer currentPage;

    public void initPage(Integer currentPage, Integer pageSize, Query query) {
        pageSize = pageSize == 0 ? 20 : pageSize;
        query.limit(pageSize);
        query.skip((currentPage - 1) * pageSize);
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    public PageHelper<T> pageHelper(long total, List<T> list) {
        return new PageHelper<>(this.currentPage, total, this.pageSize, list);
    }

    public PageHelper<T> pageHelper(List<T> list) {
        return new PageHelper<>(this.currentPage, this.pageSize, list);
    }

    public PageHelper<T> pageHelper(long currentPage, long total, long pageSize, List<T> list) {
        return new PageHelper<>(currentPage, total, pageSize, list);
    }

    public PageHelper<T> pageHelper(long currentPage, long pageSize, List<T> list) {
        return new PageHelper<>(currentPage, pageSize, list);
    }
}

